﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using System.Configuration;

using Happy.Web.Mvc.Utils;
using Happy.Web.Mvc.FileUpload;

namespace Happy.Web.Mvc.CkEditor
{
    /// <summary>
    /// 集成 CKEditor，提供文件上传功能。
    /// </summary>
    public class CkEditorFileUploadController : Controller
    {
        private static readonly string _CkEditor_Upload_Files_Relative_Url_Setting_Name = "CkEditor_Upload_Files_Relative_Url";
        private static readonly string _CkEditor_Upload_Files_Relative_Url_Default_Value = "/CkEditor_Upload_Files";
        private static readonly string _CkEditor_Upload_Image_Extension_Reg_Setting_Name = "CkEditor_Upload_Image_Extension_Reg";
        private static readonly string _CkEditor_Upload_Image_Extension_Reg_Url_Default_Value = @"\.[(jpeg)|(jpg)|(gif)|(png)|(bmp)]";

        /// <summary>
        /// 上传附件。
        /// </summary>
        public ActionResult UploadLink(string CKEditorFuncNum)
        {
            var fileUploader = CreateFileUploader("Links");

            return Upload(CKEditorFuncNum, fileUploader);
        }

        /// <summary>
        /// 上传图片。
        /// </summary>
        public ActionResult UploadImage(string CKEditorFuncNum)
        {
            var fileUploader = CreateFileUploader("Images");

            var allowedFileExtensionReg = ConfigurationManager.AppSettings[_CkEditor_Upload_Image_Extension_Reg_Setting_Name] ?? _CkEditor_Upload_Image_Extension_Reg_Url_Default_Value;
            fileUploader.AllowedFileExtensionReg = allowedFileExtensionReg;

            return Upload(CKEditorFuncNum, fileUploader);
        }

        private static FileUploader CreateFileUploader(string childDirectory)
        {
            var uploadFilesRelativeUrl = ConfigurationManager.AppSettings[_CkEditor_Upload_Files_Relative_Url_Setting_Name] ?? _CkEditor_Upload_Files_Relative_Url_Default_Value;

            return new FileUploader(new GuidSavedFileNameProvider(uploadFilesRelativeUrl + "/" + childDirectory));
        }

        private ActionResult Upload(string ckeditorFuncNum, FileUploader fileUploader)
        {
            try
            {
                var fileUploadResult = fileUploader.Upload(this.Request.Files[0]);
                var url = UrlUtil.GetRelativeUrl(fileUploadResult.SavedFileName);

                return this.UploadResult(ckeditorFuncNum, url, "上传成功");
            }
            catch (Exception ex)
            {
                return this.UploadResult(ckeditorFuncNum, String.Empty, ex.Message);
            }
        }

        private ActionResult UploadResult(string ckeditorFuncNum, string url, string message)
        {
            return this.Content(string.Format("<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction({0}, '{1}', '{2}');</script>", ckeditorFuncNum, url, message));
        }
    }
}
